package com.jiang.mybatis.mapper;


import com.jiang.mybatis.config.mybatis.JacksonTypeHandler;
import com.jiang.mybatis.entity.User;
import com.jiang.mybatis.view.dto.UserSearchDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 用户表 Mapper 接口
 * </p>
 *
 * @author jiang
 * @since 2022-10-27
 */
@Mapper
public interface UserMapper {

    @Insert("<script>" +
            "INSERT INTO `user` (`name`, `password`, `gender`, `label`, `other_info`, `create_time`, `update_time`) " +
            "VALUES (#{name}, #{password}, #{gender.value}, " +
            "#{label, typeHandler=com.jiang.mybatis.config.mybatis.JacksonTypeHandler}, " +
            "#{otherInfo, typeHandler=com.jiang.mybatis.config.mybatis.JacksonTypeHandler}, " +
            "#{createTime}, #{updateTime})" +
            "</script>")
    void insert(User user);

    @Delete("DELETE FROM user WHERE id = #{id}")
    void deleteById(Long id);

    void updateById(User user);

    @Results({
            @Result(column = "label", property="label", typeHandler = JacksonTypeHandler.class),
            @Result(column = "other_info", property="otherInfo", typeHandler = JacksonTypeHandler.class)
    })
    @Select("SELECT * FROM user WHERE id = #{id}")
    User selectById(Long id);

    @Results({
            @Result(column = "label", property="label", typeHandler = JacksonTypeHandler.class),
            @Result(column = "other_info", property="otherInfo", typeHandler = JacksonTypeHandler.class)
    })
    @Select("SELECT * FROM user")
    List<User> page(UserSearchDTO dto);

}
